package com.zp.resource.common;

import java.util.ArrayList;
import java.util.List;

import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.QueryParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;

import org.springframework.stereotype.Component;

import com.sun.jersey.spi.resource.Singleton;
import com.zp.bean.common.ResultCommonVo;
import com.zp.bean.common.ResultContentTypeVo;
import com.zp.domain.ContentType;
import com.zp.util.Constant;
import com.zp.util.MyException;

/**
 * ctype services
 * 
 * @author chenlijun08@gmail.com
 * 
 * @version 1.0
 * 
 * @date 2014/2/12
 * 
 */
@Component
@Path("/rest/ctype")
@Singleton
public class ContentTypeServices extends BaseServices {

	/**
	 * add one ContentType path:/ctype/addType Http:POST
	 * 
	 * @param ctype
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@POST
	@Path("/addType")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response addType(ContentType ctype) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.save(ctype);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * update one ContentType path:/ctype/updateType Http:PUT
	 * 
	 * @param ctype
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0}
	 */
	@PUT
	@Path("/updateType")
	@Consumes(MediaType.APPLICATION_JSON)
	@Produces(MediaType.APPLICATION_JSON)
	public Response updateType(ContentType ctype) {
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.update(ctype);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * delete ContentType by id path:/ctype/deleteType Http:DELETE
	 * 
	 * @param typeId
	 *            primary key of ContentType
	 * 
	 * @return Response, ResultCommonVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":1}
	 */
	@DELETE
	@Path("/deleteType")
	@Produces(MediaType.APPLICATION_JSON)
	public Response deleteType(@QueryParam("typeId") String typeId) {
		logger.info("deleteType called typeId:" + typeId);
		ResultCommonVo result = new ResultCommonVo();
		try {
			jpaDao.deleteById(ContentType.class, typeId);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * get ContentType by id path:/ctype/getType Http:POST
	 * 
	 * @param typeId
	 *            primary key of ContentType
	 * 
	 * @return Response, ResultContentTypeVo with JSON String
	 *         like:{"code":268,"message":"OK","totalCount":0,"type":{"id":
	 *         "402881e744791cd2014479213e260005"
	 *         ,"name":"心理关爱","code":"cm01","parentCode":"cm"},"listVo":null}
	 */
	@POST
	@Path("/getType")
	@Produces(MediaType.APPLICATION_JSON)
	public Response getTypeById(@QueryParam("typeId") String typeId) {
		logger.info("getTypeById called typeId:" + typeId);
		ResultContentTypeVo result = new ResultContentTypeVo();
		try {
			ContentType ctype = (ContentType) jpaDao.findById(
					ContentType.class, typeId);
			result.setType(ctype);
			result.setCode(Constant.SUCCESS_CODE);
			result.setMessage(Constant.OK);
		} catch (MyException e) {
			result.setCode(Constant.FAILE_CODE);
			result.setMessage(e.getMessage());
			e.printStackTrace();
		}
		return Response.ok(result).build();
	}

	/**
	 * 
	 * get ctype with page parameters and search condition path:/ctype/getTypes
	 * Http:POST
	 * 
	 * @param pageNo
	 * 
	 * @param pageSize
	 * 
	 * @param codeName
	 *            if ignored the value is null
	 * 
	 * @return Response, ResultContentTypeVo with JSON String
	 *         like:{"code":268,"message"
	 *         :"OK","totalCount":1,"type":null,"listVo"
	 *         :[{"id":"402881e744791cd2014479213e260005"
	 *         ,"name":"心理关爱","code":"cm01","parentCode":"cm"}]}
	 */
	@POST
	@Path("/getTypes")
	@Produces({ MediaType.APPLICATION_JSON })
	public Response getTypeByCodeName(@QueryParam("pageNo") int pageNo,
			@QueryParam("pageSize") int pageSize,
			@QueryParam("codeName") String codeName) {
		logger.info("getTypeByCodeName called json pageNo:" + pageNo
				+ " pageSize:" + pageSize + " codeName:" + codeName);
		String jpql = "from ContentType where code=? order by code asc";
		String countSql = "select count(id) from ContentType where code=? order by code asc";
		List<?> list = null;
		ResultContentTypeVo result = new ResultContentTypeVo();
		List<ContentType> lt = new ArrayList<ContentType>();
		result.setCode(Constant.SUCCESS_CODE);
		result.setMessage(Constant.OK);
		if ("".equals(codeName) || codeName == null) {
			jpql = "from ContentType order by code asc";
			countSql = "select count(id) from ContentType order by code asc";
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, null);
				long totalCount = this.jpaDao.getCount(countSql, null);
				result.setTotalCount(totalCount);
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		} else {
			Object[] param = { codeName };
			try {
				list = this.jpaDao.getResultData(pageNo, pageSize, jpql, param);
				long totalCount = this.jpaDao.getCount(countSql, param);
				result.setTotalCount(totalCount);
			} catch (MyException e) {
				result.setCode(Constant.FAILE_CODE);
				result.setMessage(e.getMessage());
				e.printStackTrace();
			}
		}
		if (list != null && list.size() > Constant.ZERO) {
			for (Object o : list) {
				ContentType ctype = (ContentType) o;
				lt.add(ctype);
			}
			result.setListVo(lt);

		}
		return Response.ok(result).build();
	}
}
